﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bitstar.YahooImport;
using Ciripescu_WebRole.BL;
using Ciripescu_WebRole.Entities;

// TODO: de trimis mail la cel care a fost adaugat in lista sa stie
namespace Ciripescu_WebRole
{
    public partial class invita : Page
    {
        public string UserName
        {
            get { return (string)Session["UserName"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // pune buttonul pe default submit
            txtYahooPassword.Attributes.Add("onkeypress", "return clickButton(event,'" + btnYahooImport.ClientID + "')");
        }

        protected void btnYahooImport_Click(object sender, EventArgs e)
        {
            YahooContactImport import = new YahooContactImport();
            string user = txtYahooUser.Text;
            string password = txtYahooPassword.Text;
            if (user.Contains("@")) user = (user.Split('@'))[0];
            MailContactList list = new MailContactList();
            if (user == "" || password == "")
            {
                errors.InnerHtml = "Intai completeaza userul si parola de yahoo!";
                return;
            }
            try
            {
                list = Utils.ValidateContactList(import.ExtractContacts(user, password),UserName);
            }
            catch (InvalidPassOrUsernameException)
            {
                errors.InnerHtml = "User sau parola gresita";
                return;
            }
            catch (UnexpectedResponseException ee)
            {
                errors.InnerHtml = "Eroare interna! Va rugam incercati mai tarziu";
                Utils.ReportCriticalError(ee.ToString() + "Login: \r\nUser: " + txtYahooUser.Text + "\r\nPass: "+txtYahooPassword.Text);
                return;
            }
            catch (Exception eee)
            {
                return;
            }
            Session["ContactList"] = list;
            //Response.Redirect("import.aspx");
            Response.Redirect("/import/");
        }
    }
}
